# -*- coding: utf-8 -*-
"""
摄像头原始尺寸：480 * 640 
拍照界面取3:2 --> 384:256 --> 768:512
摄像头使用时最好打开补光灯。

******主要修改的参数为 mode==1 时的 w值。*******
4张图片的描述：
(1)img_path:       raw_img.jpg    #摄像头拍照得到的图片 #h*w*c = 768 * 800 * 3
(2)img_save_path:  pre_img.jpg    #要在软件上显示的图片
(3)img_save2_path: model_img.jpg  #要传给模型的图片
(4)fn_pi_main.py:  final_img.jpg  #模型处理完之后的图片
"""

import cv2
import time

def rotate_img(img):
    '''对图片进行90旋转'''
    r_angle = 90
    h, w = img.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, r_angle, 1.0)
    rotated = cv2.warpAffine(img, M, (h, w))
#    rotated = rotated[:rotated.shape[0]-3, 3:rotated.shape[1]-3, :]
    return rotated

def crop_img(img_input, w=128, h=192, f_img_w=512,f_img_h=768):
    center_x = img_input.shape[1]//2
    center_y = img_input.shape[0]//2
    
    x0 = center_x - w
    x1 = center_x + w
    
    y0 = center_y - h
    y1 = center_y + h
    
    img_out = img_input[y0:y1,x0:x1,:] #[h,w,channel]
    #print('img_out shape:', img_out.shape)
    img_out = cv2.resize(img_out,(f_img_w,f_img_h),interpolation=cv2.INTER_AREA)
#    img_out = cv2.resize(img_out, (0, 0), fx=scale_h, fy=scale_h, interpolation=cv2.INTER_CUBIC)
#    img_out = cv2.resize(img_out, (0, 0), fx=scale_h, fy=scale_h, interpolation=cv2.INTER_AREA)
    return img_out
    
def img_save(img_input,save_path='./test.jpg'):
    cv2.imwrite(save_path,img_input)
    
    
def camera_test(times=20): #打开摄像头60s
    cap = cv2.VideoCapture(0)
    t1 = time.time()
    t2 = time.time()
    while (t2 - t1 < times): 
        t2 = time.time()
        ret1, frame1 = cap.read()
        raw_img = frame1.copy()
        print(raw_img.shape)
        img2 = crop_img(raw_img)
        img_save(img2)
        cv2.imshow('frame', img2)
        cv2.waitKey(1)
    cv2.destroyAllWindows()
    cap.release() #释放摄像头
    print(ret1)
    print(t2-t1)



def fn_img_preprocess(root1_dir, ret,frame,img_mode): 
    img_save_path =  root1_dir + 'pre_img.jpg'    
    img_save2_path = root1_dir + 'model_img.jpg' 
#    point_color = (0, 255, 0) # BGR
#    thickness = 4             #线的粗细
    if ret:   
        raw_img = frame.copy()
#        print('raw_img shape:', raw_img.shape)
        raw_img = rotate_img(raw_img)
        
        if raw_img.shape[1] > (raw_img.shape[0] + 10):
            raw_img_x0 = raw_img.shape[1]//2 - raw_img.shape[0]//2
            raw_img_x1 = raw_img.shape[1]//2 + raw_img.shape[0]//2
            raw_img = raw_img[:, raw_img_x0:raw_img_x1,:]
            #print('raw_img shape:', raw_img.shape)
        h1 = raw_img.shape[0]
        w1 = raw_img.shape[1]
        #scale_h = round(768/h1) #四舍五入，若图片本来很大，那么最后scale_h=0，就会出错
        scale_h = 768/h1 
        scale_h2 = h1/550
        w3 = int(w1 / scale_h2)
#        raw_img = cv2.resize(raw_img,(w2,768),interpolation=cv2.INTER_AREA)
        raw_img = cv2.resize(raw_img,(w3,550),interpolation=cv2.INTER_AREA)
#        raw_img = cv2.resize(raw_img, (0, 0), fx=scale_h, fy=scale_h, interpolation=cv2.INTER_CUBIC)
#        raw_img = cv2.resize(raw_img, (0, 0), fx=scale_h, fy=scale_h, interpolation=cv2.INTER_AREA)
        #print('照片的原始尺寸：',raw_img.shape)
        
        #cv2.imwrite(img_path,frame)
        if img_mode == 1: #模式一批改算术题
#            new_img1 = raw_img[:,0:512,:] #[h,w,channel]
            new_img1 = raw_img[:,:,:] #[h,w,channel]
            center_x = new_img1.shape[1]//2
            center_y = new_img1.shape[0]//2
            w = center_y - 10
            #h = 768
            x1 = int(center_x - w/2)
            y1 = 0
            x2 = int(center_x + w/2)
            y2 = 469
#            new_img2 = raw_img.copy()[y1:768,x1:(x2+1),:]
#            new_img2 = raw_img.copy()[:,x1:(x2+1),:]
            new_img2 =  raw_img.copy()
            '''画线'''
            #cv2.line(new_img1,(x1,y1),(x1,y2),point_color,thickness)
            #cv2.line(new_img1,(x2,y1),(x2,y2),point_color,thickness)
            
            cv2.imwrite(img_save_path,new_img1)
            cv2.imwrite(img_save2_path,new_img2)
            
        elif img_mode == 2: #模式二判断卡片式子
#            new_img1 = raw_img[0:768,0:768,:] #[h,w,channel]
            new_img1 = raw_img[:,:,:]
            new_img2 = new_img1.copy()
            center_x = new_img1.shape[1]//2
            center_y = new_img1.shape[0]//2
            #w = 768
            h = 512
            
            x1 = 0
            y1 = int(center_y - h/2)
            x2 = 767
            y2 = int(center_y + h/2)
            '''画线'''
#            cv2.line(new_img1,(x1,y1),(x2,y1),point_color,thickness)
#            cv2.line(new_img1,(x1,y2),(x2,y2),point_color,thickness)
            
            cv2.imwrite(img_save_path,new_img1)
            cv2.imwrite(img_save2_path,new_img2)

if __name__ == '__main__':
    camera_test()
    #img_mode = 2 #1 or 2
    #img_path = './raw_img.jpg'         
    #raw_img = cv2.imread(img_path)
    ##lineType = 8
    #cap = cv2.VideoCapture(0)   #打开摄像头
    #ret, frame = cap.read()     #读取摄像头状态和帧
                
    #fn_img_preprocess(ret,frame,img_mode)
                
    #cv2.imshow("Image", new_img1)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()
    #cap.release() #关闭摄像头



















